//
//  OrientationViewController.m
//  Estate
//
//  Created by heyong on 16/6/3.
//  Copyright © 2016年 ttsx. All rights reserved.
//

// 位置信息
#define POSITION @"addressName"
// 图片ID
#define IMAGE @"imageName"
// 主键ID
#define SELECTID @"ID"
// 模型
#define MODEL @"aModel"

#import "LocationView_jl.h"

#import "OrientationViewController.h"
#import <BaiduMapAPI_Base/BMKBaseComponent.h>//引入base相关所有的头文件
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//引入定位功能所有的头文件

@interface OrientationViewController ()<BMKLocationServiceDelegate>
{
    //记录当前选择级数
    int num;
    UIButton *btn;
    LocationView_jl *views;
}

@property (strong, nonatomic) UIImageView *imageView;

// 记录各级数据源
@property (strong, nonatomic) NSMutableArray *Arr1;
@property (strong, nonatomic) NSMutableArray *Arr2;
@property (strong, nonatomic) NSMutableArray *Arr3;

// 从本地数据库获取的数据
@property (strong, nonatomic) NSMutableArray *data;
// 数据库
@property (strong, nonatomic) FMDatabase *db;
// 百度定位管理者
@property (nonatomic, strong)BMKLocationService *locService;
/**回传数据模型*/
@property (strong, nonatomic) PositionBlockModel *blockModel;


@end


@implementation OrientationViewController


// 回传数据模型
- (PositionBlockModel *)blockModel {
    if (!_blockModel) {
        _blockModel = [[PositionBlockModel alloc] init];
    }
    return _blockModel;
}

//懒加载
- (NSMutableArray *)textArr {
    if(_dataArr==nil){
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (UIView *)flagView{
    if(_flagView==nil){
        _flagView=[[UIView alloc]init];
        [_flagView addSubview:self.tableView];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
          make.top.mas_offset(0).with.left.mas_offset(0).with.right.mas_offset(0).with.bottom.mas_offset(0);
        }];
    }
    return _flagView;
}

- (UIImageView *)imageView {
    if(_imageView==nil){
        _imageView=[[UIImageView alloc]initWithFrame:CGRECTS(0, 0, 375, 667)];
    }
    return _imageView;
}

-(UILabel *)addlab{
    if(_addlab==nil){
        _addlab=[[UILabel alloc]init];
        _addlab.font=[UIFont systemFontOfSize:13];
        _addlab.textColor=[UIColor blackColor];
        _addlab.textAlignment=1;
    }
    return _addlab;
}

-(UITableView *)tableView{
    if(_tableView==nil){
        _tableView=[[UITableView alloc]init];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];
    }
    return _tableView;
}

- (NSMutableArray *)addressArr{
    if(_addressArr==nil){
        _addressArr=[NSMutableArray array];
    }
    return _addressArr;
}

- (FMDatabase *)db {
    if (!_db) {
        _db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    }
    [_db open];
    return _db;
}

#pragma mark -试图加载
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    [self createView];
    
    if (!self.hC) {
        NSNumber *patentID = [[NSNumber alloc] initWithInt:0];
        self.dataArr1 = [self createDataSource:(NSString *)patentID];
        self.dataArr = [NSMutableArray arrayWithArray:self.dataArr1];
        if(self.dataArr.count==0){
            [LSUtility showToastWithMessage:@"请在pc端配置位置信息" inView:self.view Andtime:1.2];
        }
    } else {
        NSNumber *patentID = [[NSNumber alloc] initWithInt:0];
        self.dataArr1 = [self createDataSource:(NSString *)patentID];
        self.dataArr = [NSMutableArray arrayWithArray:self.dataArr1];
        NSDictionary *dic=[self.dataArr1 lastObject];
        self.addressModel= dic[@"aModel"];
        self.keyStr= self.addressModel.ADDRESS_NAME;
        [self.addressArr addObject:self.keyStr];
        
        // 点击的坐标
        views.selectedX = self.selectedX;
        views.selectedY = self.selectedY;
        views.scale = self.scale;
        
        num = self.number;
        if (num == 3) {
            self.dataArr = [NSMutableArray arrayWithArray:self.dataArr3];
        } else if (num == 2) {
            self.dataArr = [NSMutableArray arrayWithArray:self.dataArr2];
        } else if (num == 1) {
            self.dataArr = [NSMutableArray arrayWithArray:self.dataArr1];
        } else if (num == 4) {
            self.dataArr = [NSMutableArray arrayWithArray:self.dataArr4];
        }
    }
    [self.tableView reloadData];
}
- (void)createView {
    self.view.backgroundColor=[UIColor whiteColor];
    // 设置头部选择视图
    UIButton *titleBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 260, 30)];
    titleBtn.backgroundColor=[UIColor whiteColor];
    titleBtn.layer.cornerRadius=5.;
    [titleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [titleBtn setTitle:@"请选择位置" forState:UIControlStateNormal];
    titleBtn.tag=101;
    [titleBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView=titleBtn;
    
    // 上一级按钮
    UIButton *rightBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 30)];
    rightBtn.titleLabel.font=[UIFont systemFontOfSize:14.];
    [rightBtn setTitle:@"上一级" forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.tag=102;
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    
    // 添加弹出视图
    __weak typeof(self.view) weakSelf=self.view;
    
    [self.view addSubview:self.flagView];
    [self.flagView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.mas_centerX);
        make.top.mas_offset(64);
        make.width.mas_equalTo(weakSelf.mas_width).multipliedBy(0.7);
        make.height.mas_equalTo(weakSelf.mas_height).multipliedBy(0.3);
    }];
    //默认隐藏
     self.flagView.hidden=YES;

    //添加选择信息栏
    [self.view addSubview:self.addlab];
    [self.addlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.mas_centerX);
        make.top.mas_equalTo(64);
        make.width.mas_equalTo(DEF_WIDTH(weakSelf));
        make.height.mas_equalTo(30);
    }];
   
    //默认选中等级
    num=1;
    if (!self.hC) {
        [self.addressArr addObject:@""];
    }
    
     views=[[LocationView_jl alloc]init];
    views.successBlock = ^{
        // 计算leftDistance
        double rate = views.selectedX/views.BLscale/DEF_SCREEN_WIDTH;
        int leftDistance = self.addressModel.LEFT_NAME + self.addressModel.DISTANCE_OF_LEFTANDRIGHT * rate;
        // 计算leftDistanceStr
        NSString *leftDistanceStr = [NSString stringWithFormat:@"K%d+%d", leftDistance/1000, leftDistance%1000];
        self.addlab.text = [NSString stringWithFormat:@"%@ %@",self.addressArr.lastObject,leftDistanceStr];

    };
    [self.view addSubview:views];
    [views mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0).with.right.mas_offset(0);
        make.top.mas_equalTo(self.addlab.mas_bottom).mas_offset(0);
        make.bottom.mas_offset(-50);
    }];
    
    //创建底部视图
    UIView *buttonView=[[UIView alloc]initWithFrame:CGRECTS(0, 617, 375, 50)];
    buttonView.backgroundColor=[[UIColor blackColor]colorWithAlphaComponent:0.4];
    [self.view addSubview:buttonView];
    
    // 添加记录
    UIButton *qtBtn=[[UIButton alloc]init];
    [buttonView addSubview:qtBtn];
    __weak typeof(buttonView) weakButton=buttonView;
    qtBtn.tag=105;
    [qtBtn setBackgroundColor:[UIColor colorWithRed:0.69 green:0.27 blue:0.12 alpha:1.00]];
    qtBtn.titleLabel.font=[UIFont systemFontOfSize:13.];
    [qtBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    [qtBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right .mas_offset(-5);
        make.top.mas_offset(5);
        make.width.mas_equalTo(weakButton.mas_width).multipliedBy(0.24);
        make.height.mas_equalTo(weakButton.mas_height).mas_offset(-10);
    }];
    if([self.flagStr isEqualToString:@"jilu"]){
        [qtBtn setTitle:@"确  定" forState:UIControlStateNormal];
        
    } else {
        [qtBtn setTitle:@"添加记录" forState:UIControlStateNormal];
    }
    
    [self.view bringSubviewToFront:self.flagView];
}


// 获取数据
- (NSMutableArray *)createDataSource:(NSString *)patentID {
    // 获取项目ID
    NSString *proID = DEF_PERSISTENT_GET_OBJECT(@"proid");
    
    NSString *Sql = [NSString stringWithFormat:@"SELECT * FROM ADDRESS WHERE PATENT_ID = '%@' AND PRO_ID = '%@' ORDER BY ADDRESS_NAME", patentID, proID];
    
    // 获取查询的结果集
    FMResultSet *result = [self.db executeQuery:Sql];
    NSMutableArray *moedlArr = [NSMutableArray array];
    while ([result next]) {
        AddressModel *aModel = [[AddressModel alloc] init];
        
        aModel.ID = [result stringForColumn:@"ID"];
        aModel.PRO_ID = [result stringForColumn:@"PRO_ID"];
        aModel.PATENT_ID = [result stringForColumn:@"PATENT_ID"];
        aModel.ADDRESS_NAME = [result stringForColumn:@"ADDRESS_NAME"];
        aModel.ADDRESS_TYPE = [result stringForColumn:@"ADDRESS_TYPE"];
        aModel.A__LONG = [result doubleForColumn:@"A__LONG"];
        aModel.A__LAT = [result doubleForColumn:@"A__LAT"];
        aModel.A__HEIGHT = [result doubleForColumn:@"A__HEIGHT"];
        aModel.B__LONG = [result doubleForColumn:@"B__LONG"];
        aModel.B__LAT = [result doubleForColumn:@"B__LAT"];
        aModel.B__HEIGHT = [result doubleForColumn:@"B__HEIGHT"];
        aModel.C__LONG = [result doubleForColumn:@"C__LONG"];
        aModel.C__LAT = [result doubleForColumn:@"C__LAT"];
        aModel.C__HEIGHT = [result doubleForColumn:@"C__HEIGHT"];
        aModel.D__LONG = [result doubleForColumn:@"D__LONG"];
        aModel.D__LAT = [result doubleForColumn:@"D__LAT"];
        aModel.D__HEIGHT = [result doubleForColumn:@"D__HEIGHT"];
        aModel.STATE = [result intForColumn:@"STATE"];
        aModel.SCALE = [result stringForColumn:@"SCALE"];
        aModel.IMAGE_INFO = [result stringForColumn:@"IMAGE_INFO"];
        aModel.IMAGE_INFO_DESC = [result stringForColumn:@"IMAGE_INFO_DESC"];
        aModel.GPS_A_LAT = [result stringForColumn:@"GPS_A_LAT"];
        aModel.GPS_A_LONG = [result stringForColumn:@"GPS_A_LONG"];
        aModel.GPS_C_LAT = [result stringForColumn:@"GPS_C_LAT"];
        aModel.GPS_C_LONG = [result stringForColumn:@"GPS_C_LONG"];
        aModel.LEFT_NAME = [result intForColumn:@"LEFT_NAME"];
        aModel.RIGHT_NAME = [result intForColumn:@"RIGHT_NAME"];
        aModel.UP_NAME = [result intForColumn:@"UP_NAME"];
        aModel.DOWN_NAME = [result intForColumn:@"DOWN_NAME"];
        aModel.DISTANCE_OF_LEFTANDRIGHT = [result intForColumn:@"DISTANCE_OF_LEFTANDRIGHT"];
        aModel.DISTANCE_OF_UPANDDOWN = [result intForColumn:@"DISTANCE_OF_UPANDDOWN"];
        aModel.EXTEND_INFO = [result stringForColumn:@"EXTEND_INFO"];
        aModel.CREATE_USER_NAME = [result stringForColumn:@"CREATE_USER_NAME"];
        aModel.CREATE_TIME = [result stringForColumn:@"CREATE_TIME"];
        aModel.CREATE_IP = [result stringForColumn:@"CREATE_IP"];
        aModel.MODIFY_USER_NAME = [result stringForColumn:@"MODIFY_USER_NAME"];
        aModel.MODIFY_IP = [result stringForColumn:@"MODIFY_IP"];
        aModel.MODIFY_TIME = [result stringForColumn:@"MODIFY_TIME"];
        
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:aModel.ADDRESS_NAME, POSITION, aModel.IMAGE_INFO, IMAGE, aModel.ID, SELECTID, aModel, MODEL, nil];
        [moedlArr addObject:dic];
    }
    [_db close];
    return moedlArr;
}

#pragma  mark clickBtn
- (void)clickBtn:(UIButton *)sender{

    if(sender.tag==101){
        self.flagView.hidden=NO;
        self.addlab.hidden=YES;
    }else if(sender.tag==102){
        self.hC = NO;
        num--;
        NSLog(@"上一级num:%d",num);
        [self.addressArr removeLastObject];
        self.addlab.text = self.addressArr.lastObject;
        self.keyStr = self.addressArr.lastObject;
        views.selectedX = 0;
        views.selectedY = 0;
        views.scale = 0.0;
        
        if(num >= 1){
            if (num == 1) {
                self.dataArr = self.dataArr1;
                self.position = @"";
                self.aID = @"";
            } else if (num == 2) {
                self.dataArr = self.dataArr2;
                // 图片显示
                NSString *imageInfo = [self getImageInfofromID:self.firstID];
                if (imageInfo) {
                    NSString *fileName = [self checkImagePath:imageInfo];
                    UIImage *img = [LSUtility getImageByFileName2:fileName];
                    [views createUIWithImage:img ImgUrl:nil];
                }
                self.position = @"";
                self.aID = [self.firstID mutableCopy];
            } else if (num == 3) {
                self.dataArr = [NSMutableArray arrayWithArray:self.dataArr3];
                // 图片显示
                NSString *imageInfo = [self getImageInfofromID:self.secondID];
                if (imageInfo) {
                    NSString *fileName = [self checkImagePath:imageInfo];
                    UIImage *img = [LSUtility getImageByFileName2:fileName];
                    [views createUIWithImage:img ImgUrl:nil];
                }
                self.position = @"";
                self.aID = [self.secondID mutableCopy];
            } else if (num == 4) {
                self.dataArr = [NSMutableArray arrayWithArray:self.dataArr4];
                // 图片显示
                NSString *imageInfo = [self getImageInfofromID:self.thirdID];
                if (imageInfo) {
                    NSString *fileName = [self checkImagePath:imageInfo];
                    UIImage *img = [LSUtility getImageByFileName2:fileName];
                    [views createUIWithImage:img ImgUrl:nil];
                }
                self.position = @"";
                self.aID = [self.secondID mutableCopy];
            }
            [self.tableView reloadData];
        }else{
            num=1;
        }
        
    } else if(sender.tag==105){
        
        if([self.flagStr isEqualToString:@"jilu"]){
            //得到传出的坐标点
            if (views.selectedX != 0 && views.selectedY != 0 && self.addressModel) {
                
            } else {
                [LSUtility showToastWithMessage:@"请选择位置" inView:self.view Andtime:1.];
                return;
            }
            if(self.aID == nil||self.keyStr==nil){
                [LSUtility showToastWithMessage:@"请选择位置" inView:self.view Andtime:1.];
            } else {
                // 回传参数
                self.blockModel.keyStr = DEFISNUL(self.keyStr);
                self.blockModel.position = DEFISNUL(self.position);
                self.blockModel.dataArr1 = self.dataArr1;
                self.blockModel.dataArr2 = self.dataArr2;
                self.blockModel.dataArr3 = self.dataArr3;
                self.blockModel.dataArr4 = self.dataArr4;
                self.blockModel.number = num;
                self.blockModel.addressArr = self.addressArr;
                self.blockModel.aID = self.aID;
                self.blockModel.firstID = self.firstID;
                self.blockModel.secondID = self.secondID;
                self.blockModel.thirdID = self.thirdID;
                self.blockModel.selectedX = views.selectedX;
                self.blockModel.selectedY = views.selectedY;
                self.blockModel.scale = views.scale;
                self.blockModel.BLscale = views.BLscale;
                
                // 位置详细信息ADD_INFO
                NSMutableDictionary *addInfoDic = [NSMutableDictionary dictionary];
                [addInfoDic setObject:self.keyStr forKey:@"address"];
                if (self.blockModel.gps_long) {
                    [addInfoDic setObject:self.blockModel.gps_long forKey:@"gps_long"];
                }
                if (self.blockModel.gps_lat) {
                    [addInfoDic setObject:self.blockModel.gps_lat forKey:@"gps_lat"];
                }
                [addInfoDic setObject:@"" forKey:@"upDistance"];
                [addInfoDic setObject:@"" forKey:@"upDistanceStr"];
                [addInfoDic setObject:@(views.selectedX) forKey:@"x"];
                [addInfoDic setObject:@(views.selectedY) forKey:@"y"];
                // 计算leftDistance
                double rate = views.selectedX/views.BLscale/DEF_SCREEN_WIDTH;
                int leftDistance = self.addressModel.LEFT_NAME + self.addressModel.DISTANCE_OF_LEFTANDRIGHT * rate;
                [addInfoDic setObject:[NSString stringWithFormat:@"%d", leftDistance] forKey:@"leftDistance"];
                // 计算leftDistanceStr
                NSString *leftDistanceStr = [NSString stringWithFormat:@"K%d+%d", leftDistance/1000, leftDistance%1000];
            
#pragma mark 此处变动 判断如果是交建项目就有 leftDistanceStr 没有赋值为空
                NSString *proType = DEF_PERSISTENT_GET_OBJECT(@"proType");
                if ([proType isEqualToString:@"3"]) {
                    [addInfoDic setObject:leftDistanceStr forKey:@"leftDistanceStr"];
                }else{
                    [addInfoDic setObject:@"" forKey:@"leftDistanceStr"];
                }
             
                
                NSData *addInfoData = [NSJSONSerialization dataWithJSONObject:addInfoDic options:0 error:nil];
                NSString *addInfoStr = [[NSString alloc] initWithData:addInfoData encoding:NSUTF8StringEncoding];
               
                // 位置值传入上一界面
                if(self.returnPlaceInfoBlock){
                    self.returnPlaceInfoBlock(self.blockModel, self.addressModel, addInfoStr);

                }
                [self.navigationController popViewControllerAnimated:YES];
            }
        } else {
            JLAddJLViewController *addCtr=[[JLAddJLViewController alloc]init];
            addCtr.flag=@"add";
            addCtr.isEdit=YES;
            addCtr.selectedAssetURLs=self.imageArray;
            
//          self.position = [self calculateAddressInfo:self.addressModel];
            //迭代新需求 跳转到记录添加页面
            addCtr.define=@"RECORD_01";
            addCtr.locations=DEFISNUL(self.keyStr);
            addCtr.locationId=DEFISNUL(self.aID);
            addCtr.addressInfo=DEFISNUL(self.position);
            [self.navigationController pushViewController:addCtr animated:YES];
        }
    }
}

// 根据ID获取imageInfo
- (NSString *)getImageInfofromID:(NSString *)addressID {
    NSString *sql = [NSString stringWithFormat:@"SELECT IMAGE_INFO FROM ADDRESS WHERE ID = '%@'", addressID];
    FMResultSet *result = [self.db executeQuery:sql];
    NSString *imageInfo;
    while ([result next]) {
        imageInfo = [result stringForColumn:@"IMAGE_INFO"];
    }
    return imageInfo;
}

#pragma mark- UITableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section==0){
        return 1;
    } else {
        return self.dataArr.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    self.addlab.text = [self.addressArr lastObject];
    static NSString *acell=@"acell";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:acell];
    if(!cell){
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:acell];
    }
    if (indexPath.section==0) {
        cell.textLabel.text=[NSString stringWithFormat:@"请选择，共[%li]条数据", self.dataArr.count];
    } else {
        if(self.dataArr.count != 0){
            //得到dic
            NSDictionary *dic = self.dataArr[indexPath.row];
            cell.textLabel.text = dic[@"addressName"];
        }
    }
    // 默认显示显示的图片
    if(self.dataArr1.count>0){
        if (num == 1) {
            NSDictionary *defaultdic = self.dataArr1[0];
            NSString *fileName = [self checkImagePath:defaultdic[@"imageName"]];
            UIImage *img = [LSUtility getImageByFileName2:fileName];
            [views createUIWithImage:img ImgUrl:nil];
        } else if (self.hC && num >= 2) {
            // 图片显示
            NSString *imageInfo = [self getImageInfofromID:self.aID];
            NSString *fileName = [self checkImagePath:imageInfo];
            UIImage *img = [LSUtility getImageByFileName2:fileName];
            if (img) {
                [views createUIWithImage:img ImgUrl:nil];
            } else {
                if ([self.aID isEqualToString:self.thirdID]) {
                    imageInfo = [self getImageInfofromID:self.secondID];
                    fileName = [self checkImagePath:imageInfo];
                    img = [LSUtility getImageByFileName2:fileName];
                    
                } else if ([self.aID isEqualToString:self.secondID]) {
                    imageInfo = [self getImageInfofromID:self.firstID];
                    fileName = [self checkImagePath:imageInfo];
                    img = [LSUtility getImageByFileName2:fileName];
                    
                } else if ([self.aID isEqualToString:self.firstID]) {
                    NSDictionary *defaultdic = self.dataArr1[0];
                    fileName = [self checkImagePath:defaultdic[@"imageName"]];
                    img = [LSUtility getImageByFileName2:fileName];
                }
                [views createUIWithImage:img ImgUrl:nil];
            }
        }
        if (self.hC && views.selectedX && views.selectedY) {
            [views getX:views.selectedX withY:views.selectedY withScale:self.scale];
        }
    }
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.hC = NO;
    if(indexPath.section==1){
        num++;
        self.flagView.hidden=YES;
        self.addlab.hidden=NO;
        
        if (num == 2) {
            // 查询获取数据
            NSDictionary *dic = self.dataArr[indexPath.row];
            // 图片显示
            NSString *fileName = [self checkImagePath:dic[@"imageName"]];
            UIImage *img = [LSUtility getImageByFileName2:fileName];
        
            [views createUIWithImage:img ImgUrl:nil];
            
            // 记录当前点击的ID
            self.aID = dic[@"ID"];
            self.firstID = [dic[@"ID"] mutableCopy];
            // 显示位置
            _keyStr = dic[@"addressName"];
            // 历史添加到数组
            [self.addressArr addObject:_keyStr];
            
            // 记录被点击的数据
            self.addressModel = dic[@"aModel"];
            self.dataArr = [self createDataSource:dic[@"ID"]];
            // 记录本级数据源
            self.dataArr2 = [NSMutableArray arrayWithArray:self.dataArr];
            
        } else if (num == 3) {
            // 查询获取数据
            NSDictionary *dic = self.dataArr[indexPath.row];
            // 图片显示
            NSString *fileName = [self checkImagePath:dic[@"imageName"]];
            UIImage *img = [LSUtility getImageByFileName2:fileName];
            [views createUIWithImage:img ImgUrl:nil];
            // 记录当前点击的ID
            self.aID = dic[@"ID"];
            self.secondID = dic[@"ID"];
            // 显示位置
            _keyStr = self.addressArr.lastObject;
            _keyStr = [_keyStr stringByAppendingString:[NSString stringWithFormat:@"/%@",dic[@"addressName"]]];
            // 历史添加到数组
            [self.addressArr addObject:_keyStr];
            
            // 记录被点击的数据
            self.addressModel = dic[@"aModel"];
            self.dataArr = [self createDataSource:dic[@"ID"]];
            self.dataArr3 = [NSMutableArray arrayWithArray:self.dataArr];
            
        } else if (num == 4) {
            // 查询获取数据
            NSDictionary *dic = self.dataArr[indexPath.row];
            
            // 图片显示
            NSString *fileName = [self checkImagePath:dic[@"imageName"]];
            UIImage *img = [LSUtility getImageByFileName2:fileName];
            [views createUIWithImage:img ImgUrl:nil];
            
            // 记录当前点击的ID
            self.aID = dic[@"ID"];
            self.thirdID = dic[@"ID"];
            // 显示位置
            _keyStr = self.addressArr.lastObject;
            _keyStr = [_keyStr stringByAppendingString:[NSString stringWithFormat:@"/%@",dic[@"addressName"]]];
            //历史添加到数组
            [self.addressArr addObject:_keyStr];
            // 记录被点击的数据
            self.addressModel = dic[@"aModel"];
            self.dataArr = [self createDataSource:dic[@"ID"]];
            self.dataArr4 = [NSMutableArray arrayWithArray:self.dataArr];
        } else if (num == 5) {
            // 查询获取数据
            NSDictionary *dic = self.dataArr[indexPath.row];
            
            // 图片显示
            NSString *fileName = [self checkImagePath:dic[@"imageName"]];
            UIImage *img = [LSUtility getImageByFileName2:fileName];
            [views createUIWithImage:img ImgUrl:nil];
            
            // 记录当前点击的ID
            self.aID = dic[@"ID"];
            // 显示位置
            _keyStr = self.addressArr.lastObject;
            _keyStr = [_keyStr stringByAppendingString:[NSString stringWithFormat:@"/%@",dic[@"addressName"]]];
            //历史添加到数组
            [self.addressArr addObject:_keyStr];
            // 记录被点击的数据
            self.addressModel = dic[@"aModel"];
            //清除数据源
            [self.dataArr removeAllObjects];
            
        }
        //添加到显示框
        self.addlab.text=_keyStr;
        //刷新表格
        [self.tableView reloadData];
    }else{
        self.flagView.hidden=YES;
        self.addlab.hidden=NO;
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

// 查找图片路径
- (NSString *)checkImagePath:(NSString *)ID{
    NSString *sql = [NSString stringWithFormat:@"SELECT FILE_NAME, FILE_PATH FROM ATTACHMENT WHERE ID = '%@'", ID];
    FMResultSet *result = [self.db executeQuery:sql];
    NSString *fileName;
    while ([result next]) {
        fileName = [result stringForColumn:@"FILE_NAME"];
    }
    
    return fileName;
}

#pragma mark - BMK_LocationDelegate 百度地图
/**
 *定位失败后，会调用此函数
 *@param error 错误号
 */
- (void)didFailToLocateUserWithError:(NSError *)error
{
    NSLog(@"地图定位失败======%@",error);
}

//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    //存储经纬度
    self.blockModel.gps_lat = [NSString stringWithFormat:@"%f",userLocation.location.coordinate.latitude];
    self.blockModel.gps_long = [NSString stringWithFormat:@"%f",userLocation.location.coordinate.longitude];
}

//地图定位
- (BMKLocationService *)locService
{
    if (!_locService)
    {
        _locService = [[BMKLocationService alloc] init];
        _locService.delegate = self;
    }
    return _locService;
}
- (void)viewWillAppear:(BOOL)animated {
    //启动定位服务
    [self.locService startUserLocationService];
}

- (void)viewWillDisappear:(BOOL)animated {
    // 停止定位
    [self.locService stopUserLocationService];
}

@end

